﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;

using FRL.DebtChased.Types;

namespace FRL.DebtChased.Repository.PartModels
{

	/// <summary>
	/// 
	/// </summary>
	public class FinCommissionModel : BasePartModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////////
			// Attributes

			public int UserId { get; set; }
			public string UserDescription { get; set; }

			public Money Target { get; set; }

			public Money Invoiced { get; set; }
			public Money ToBeInvoiced { get; set; }
			public Money ClientFunds { get; set; }
			public Money AgentFees { get; set; }
			public Money SolicitorFees { get; set; }
			public Money ConfirmedToday { get; set; }

			private Money m_mnToGo;
			public Money ToGo
			{
				get { return m_mnToGo; }
			}

			private Money m_mnSubTotal;
			public Money SubTotal
			{
				get { return m_mnSubTotal; }
			}

			/////////////////////////////////////////////////////////////
			// Operations

			public void Recalc()
			{

				// Calculate sub-total
				m_mnSubTotal = this.Target + this.Invoiced +
					this.ToBeInvoiced + this.ClientFunds +
					this.AgentFees + this.SolicitorFees;

				// Calculate the amount remaining
				m_mnToGo = Target - m_mnSubTotal;

			}

		}

		/////////////////////////////////////////////////////////////
		// Enumerations

		public enum eScope
		{
			TopLevel,
			Manager,
			User
		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		public Money TotalSubTotal { get; set; }
		public Money TotalToGo { get; set; }
		public Money TotalConfirmedToday { get; set; }

		/////////////////////////////////////////////////////////////
		// Construction

		public FinCommissionModel( DebtChasedEntities ent, eScope Scope, int iUserId )
		{
			
		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void Recalc()
		{

			// Loop through elements
			for (int i1 = 0; i1 < m_elements.Count; i1++)
			{

				// Recalc element
				m_elements[i1].Recalc();

				// If this is the first element...
				if (i1 == 0)
				{
					this.TotalSubTotal = new Money(m_elements[i1].SubTotal);
					this.TotalToGo = new Money(m_elements[i1].ToGo);
					this.TotalConfirmedToday = new Money(m_elements[i1].SubTotal);
				}
				else
				{
					this.TotalSubTotal += m_elements[i1].SubTotal;
					this.TotalToGo += m_elements[i1].ToGo;
					this.TotalConfirmedToday += m_elements[i1].SubTotal;
				}

			}

		}

	}

}